package cn.btks.resultintegration.achievements.controller;

import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.util.JwtUtil;
import cn.btks.commonserver.valid.SystemLog;
import cn.btks.resultintegration.achievements.pojo.Achievements;
import cn.btks.resultintegration.achievements.pojo.TextAchievements;
import cn.btks.resultintegration.achievements.service.TextAchievementsService;
import cn.btks.resultintegration.feign.FeignObject.Unit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * @Author lijun
 * @Date 2021/1/6 12:20
 */
@Slf4j
@RestController
@RequestMapping("api/v1.0/resultIntegration/achievements")
@Api(tags = "成果整合模块")
@Validated
public class TextAchievementsController {

    @Autowired
    TextAchievementsService textAchievementsService;

    @GetMapping("/dataPresentation/text/findByUnitCode/{unitCode}")
    @ApiOperation("根据管控单元编码查询成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "unitCode", value = "管控单元编码", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("unitCode") String unitCode) {
        TextAchievements textAchievements = textAchievementsService.findByUnitCode(unitCode);
        return textAchievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), textAchievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @GetMapping("/text/findPages")
    @ApiOperation("分页查询成果管控清单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "环境管控编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "city", value = "师", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "county", value = "团", required = false, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPages(Integer pageNumber, Integer pageSize, String code, String city, String county,HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Optional<Claims> optional = JwtUtil.getClaimsFromToken(token, JwtUtil.jwtsSecret);
        if(optional.isPresent()){
            Claims claims = optional.get();
            Object obj = claims.get("unit");
            Unit unit = JSONArray.parseObject(JSON.toJSONString(obj), Unit.class);
            if(unit != null){
                if(unit.getCityCode().equals("65")){
                    return textAchievementsService.findPage(pageNumber, pageSize, code,city,county);
                }else {
                    city = unit.getCityName();
                    return textAchievementsService.findPage(pageNumber, pageSize, code,city,county);
                }
            }
        }
        return new ResultState(StateInfo.TOKEN_ERROR.getCode(), StateInfo.TOKEN_ERROR.getMsg());
    }

    @SystemLog(type = SystemLogEnum.SAVE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/text/save",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "新增文本成果数据", notes = "新增文本成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "unitCode", value = "环境管控单元编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "unitName", value = "单元名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "division", value = "师", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "regiment", value = "团", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "unitClassify", value = "管控单元分类", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "spaceRestrain", value = "空间布局约束", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "pollutantRestrain", value = "污染物排放管控", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "environmentRestrain", value = "环境风险防控", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "resourceUtilization", value = "资源利用效率", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "unitFeature", value = "单元特点、生态功能定位", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(TextAchievements textAchievements, @ApiParam(value = "multipartFile", required = true) MultipartFile multipartFiles, HttpServletRequest request) {
        return textAchievementsService.save(textAchievements,multipartFiles,request);
    }

    @SystemLog(type = SystemLogEnum.UPLOAD_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/text/upload",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传文本成果数据", notes = "上传文本成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(@ApiParam(value = "multipartFile", required = true) MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        return textAchievementsService.upload(multipartFile,request);
    }

    @SystemLog(type = SystemLogEnum.DOWNLOAD_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @GetMapping("/export")
    @ApiOperation("导出环境管控单元清单文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "环境要素管控分区编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cityName", value = "师名称", required = false, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState export(String code,String cityName, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Optional<Claims> optional = JwtUtil.getClaimsFromToken(token, JwtUtil.jwtsSecret);
        if(optional.isPresent()){
            Claims claims = optional.get();
            Object obj = claims.get("unit");
            Unit unit = JSONArray.parseObject(JSON.toJSONString(obj), Unit.class);
            if(unit != null){
                if(unit.getCityCode().equals("65")){
                    return textAchievementsService.export(code,cityName,request);
                }else {
                    cityName = unit.getCityName();
                    return textAchievementsService.export(code,cityName,request);
                }
            }
        }
        return new ResultState(StateInfo.TOKEN_ERROR.getCode(), StateInfo.TOKEN_ERROR.getMsg());
    }
}
